package com.medical_houduan.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.medical_houduan.commons.Result;
import com.medical_houduan.dto.DrugCompanyDTO;
import com.medical_houduan.mapper.DrugCompanyMapper;
import com.medical_houduan.pojo.DrugCompany;
import com.medical_houduan.service.DrugCompanyService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class DrugCompanyServiceImpl implements DrugCompanyService {
    @Autowired
    DrugCompanyMapper dMapper;
    @Override
    public Result getCompanies(Integer currentPage, Integer pageSize, String keyword) {
        Page<DrugCompany> page = new Page<>(currentPage,pageSize);
        if(keyword==null||"".equals(keyword)) {
            //获取所有公司
            QueryWrapper<DrugCompany> wrapper = new QueryWrapper<>();
            Page<DrugCompany> pg = dMapper.selectPage(page,wrapper);
            if (pg.getRecords().isEmpty()){
                return new Result(201,"没有数据了");
            }
            return new Result(200,"已获取√",pg);
        }

        //获取包含关键字的公司
        QueryWrapper<DrugCompany> wrapper1 = new QueryWrapper<>();
        wrapper1.like("company_name",keyword);
        Page<DrugCompany> pg_key = dMapper.selectPage(page,wrapper1);
        if (pg_key.getRecords().isEmpty()){
            return new Result(202,"没有查询的数据了");
        }
        return new Result(200,"已获取√",pg_key);
    }

    @Override
    public Result addCompanyService(DrugCompanyDTO drugCompanyDTO) {
        if (drugCompanyDTO==null){
            return new Result(201,"添加的数据不能为空");
        }
        if ("".equals(drugCompanyDTO.getCompanyName())||drugCompanyDTO.getCompanyName()==null){
            return new Result(202,"添加的公司名称不能为空");
        }
        if ("".equals(drugCompanyDTO.getCompanyPhone())||drugCompanyDTO.getCompanyPhone()==null){
            return new Result(203,"添加的公司联系号码不能为空");
        }
        //属性一样，复制
        DrugCompany drugCompany = new DrugCompany();
        BeanUtils.copyProperties(drugCompanyDTO,drugCompany);
        drugCompany.setCreateTime(new Date());
        drugCompany.setUpdateTime(new Date());
        int row = dMapper.insert(drugCompany);
        if(row!=1){
            return new Result(204,"添加失败");
        }
        return new Result(200,"添加成功√");
    }

    @Override
    public Result deleteCompanyService(Long companyId) {
        if(companyId==null){
            return new Result(201,"没有指定的删除数据");
        }
        int row=dMapper.deleteById(companyId);
        if(row!=1){
            return new Result(202,"删除失败，稍后重试");
        }
        return new Result(200,"删除成功√");
    }

    @Override
    public Result modifyCompanyService(DrugCompanyDTO drugCompanyDTO) {
        if (drugCompanyDTO==null){
            return new Result(201,"修改的数据不能为空");
        }
        if(drugCompanyDTO.getCompanyId()==null){
            return new Result(201,"Id不能为空");
        }
        if ("".equals(drugCompanyDTO.getCompanyName())||drugCompanyDTO.getCompanyName()==null){
            return new Result(202,"修改的公司名称不能为空");
        }
        if ("".equals(drugCompanyDTO.getCompanyPhone())||drugCompanyDTO.getCompanyPhone()==null){
            return new Result(203,"修改的公司联系号码不能为空");
        }
        //查询公司是否存在数据库
        QueryWrapper<DrugCompany> wrapper = new QueryWrapper<>();
        wrapper.eq("company_id",drugCompanyDTO.getCompanyId());
        DrugCompany selectedCompany=dMapper.selectOne(wrapper);
        if(selectedCompany==null){
            return new Result(205,"指定修改的公司不存在数据库");
        }
        //属性一样，复制
        DrugCompany drugCompany = new DrugCompany();
        BeanUtils.copyProperties(drugCompanyDTO,drugCompany);
        drugCompany.setUpdateTime(new Date());

        //更新
        int row = dMapper.updateById(drugCompany);
        if(row!=1){
            return new Result(204,"修改失败");
        }
        return new Result(200,"修改成功√",drugCompany);
    }
}
